#include<iostream>
#include<vector>
using namespace std;
class Solution
{
    vector<int>tmp;
public:
    int reversePairs(vector<int>& nums)
    {
        tmp.resize(nums.size());
        return mergesort(nums, 0, nums.size() - 1);
    }

    int mergesort(vector<int>& nums, int left, int right)
    {
        if (left >= right) return 0;
        int ret = 0;
        int mid = (right + left) / 2;

        ret += mergesort(nums, left, mid);
        ret += mergesort(nums, mid + 1, right);
        int cur1 = left, cur2 = mid + 1, i = 0;
        while (cur1 <= mid && cur2 <= right)
        {
            if (nums[cur1] <= nums[cur2])
            {

                tmp[i++] = nums[cur1++];
            }
            else
            {
                ret += mid - cur1 + 1;
                tmp[i++] = nums[cur2++];
            }
        }
        while (cur1 <= mid)tmp[i++] = nums[cur1++];
        while (cur2 <= right) tmp[i++] = nums[cur2++];

        for (int i = left; i <= right; i++)
        {
            nums[i] = tmp[i - left];
        }
        return ret;
    }
};

int main()
{

    vector<int> nums{ 7,5,6,4 };
    Solution A;
    int count=A.reversePairs(nums);
    cout << count;
    return 0;

}